﻿using KamRemakeRemake.Common;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace KamRemakeRemake.GameObjects.MapObjects {
    public class Map : IDrawableGameObject {
        /// <summary>
        /// Private collection of all the tiles in a two-dimensional array
        /// </summary>
        private Tile[,] Tiles { get; set; }

        public int WidthInTiles { get { return this.Tiles.GetLength(0); } }
        public int HeightInTiles { get { return this.Tiles.GetLength(1); } }
        public int WidthInPixels { get { return this.WidthInTiles * Tile.TILEWIDTH; } }
        public int HeightInPixels { get { return this.HeightInTiles * Tile.TILEHEIGHT; }}

        public Map(Tile[,] tiles) {
            this.Tiles = tiles;
            // Loop through all the rows in the file
            for (int rowIndex = 0; rowIndex < this.Tiles.GetLength(0); rowIndex++) {
                // Loop through all the columns of each row
                for (int colIndex = 0; colIndex < this.Tiles.GetLength(1); colIndex++) {
                    this.Tiles[rowIndex, colIndex].Map = this;
                }
            }
        }

        /// <summary>
        /// Returns the tile thats at the given position
        /// </summary>
        public Tile GetTileByCoordinates(Vector2 vector) {
            int row = (int) vector.Y/Tile.TILEHEIGHT;
            int column = (int) vector.X/Tile.TILEWIDTH;
            
            return this.Tiles[row, column];
        }

        /// <summary>
        /// Draws the map
        /// </summary>.
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, ContentManager contentManager) {
            // Loop through all the rows in the file
            for (int rowIndex = 0; rowIndex < this.Tiles.GetLength(0); rowIndex++) {
                // Loop through all the columns of each row
                for (int colIndex = 0; colIndex < this.Tiles.GetLength(1); colIndex++) {
                    this.Tiles[rowIndex, colIndex].Draw(gameTime, spriteBatch, contentManager);
                }
            }
        }

        /// <summary>
        /// Loads the content for the map
        /// </summary>
        public void LoadContent(ContentManager contentManager) {
            // Loop through all the rows in the file
            for (int rowIndex = 0; rowIndex < this.Tiles.GetLength(0); rowIndex++) {
                // Loop through all the columns of each row
                for (int colIndex = 0; colIndex < this.Tiles.GetLength(1); colIndex++) {
                    this.Tiles[rowIndex, colIndex].LoadContent(contentManager);
                }
            }
        }

        /// <summary>
        /// Updates the map
        /// </summary>
        public void Update(GameTime gameTime) {
            
        }
    }
}
